/*******************************************************************************
 * Copyright (c) 2012, 2013 XML-Print Project.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     XML-Print Project - initial API and implementation
 ******************************************************************************/
package info.xmlprint.core.mapping;

import info.xmlprint.run.Constants;
import info.xmlprint.run.RuntimeUtil;
import info.xmlprint.ui.widgets.IGroup;
import info.xmlprint.util.FileUtil;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;

public class GeneralLayout implements Serializable, IGroup {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7278616121731114449L;
	String[] attr = new String[] { "page-layout-width", "page-layout-height", 
			"page-layout-margin-top", "page-layout-margin-bottom", "page-layout-margin-left",
			"page-layout-margin-right", "page-layout-doublesided"};
	Map<String, String> attrMap = new HashMap<String, String>();

	public GeneralLayout() {
		init();
	}

	private void init() {
		attrMap.put("page-layout-width", Constants.A4_W);
		attrMap.put("page-layout-height", Constants.A4_H);
		attrMap.put("page-layout-margin-top", Constants.margin_T);
		attrMap.put("page-layout-margin-bottom", Constants.margin_B);
		attrMap.put("page-layout-margin-left", Constants.margin_L);
		attrMap.put("page-layout-margin-right", Constants.margin_R);
		attrMap.put("page-layout-doublesided", Constants.doublesided.toString());
	}

	public void setValueByKey(String key, String value) {
		attrMap.put(key, value);
	}

	public String getValueByKey(String key) {
		String value = attrMap.get(key);
		if (value == null) {
			value = "";
		}
		return value;
	}

	@Override
	public boolean hasElement(String name) {
		return false;
	}

	public GeneralLayout objClone() {
		GeneralLayout obj = (GeneralLayout) FileUtil.objClone(this);
		return obj;
	}

	public Document getXmlDoc() {
		Document doc = DocumentHelper.createDocument();

		Element root = doc.addElement("general");

		for (String key : attr) {
			Element e = root.addElement(key);
			e.setText(getValueByKey(key));
		}

		return doc;
	}

	public GeneralLayout readDocNode(Node root) {
		// <general></general>
		GeneralLayout newGeneralLayout = new GeneralLayout();

		// TODO prüfen, ob xml kompatible ist
		{

		}

		if (root == null || !(root instanceof Node)) {
			return newGeneralLayout;
		}

		//System.out.println(root.asXML());

		for (String s : attr) {
			Node node = root.selectSingleNode(s);
			if (node != null) {
				newGeneralLayout.setValueByKey(s, node.getText());
			}
		}

		return newGeneralLayout;

	}

	/*
	 * <xsl:template name="setupPagemasters"> <fo:layout-master-set>
	 * <fo:simple-page-master master-name="odd" xsl:use-attribute-sets="DINA4"
	 * margin-top="2cm" margin-bottom="3cm" margin-left="2cm"
	 * margin-right="2cm"> <fo:region-body region-name="xsl-region-body"
	 * background-color="red" /> </fo:simple-page-master> <fo:simple-page-master
	 * master-name="even" xsl:use-attribute-sets="DINA4" margin-top="2cm"
	 * margin-bottom="3cm" margin-left="2cm" margin-right="2cm"> <fo:region-body
	 * region-name="xsl-region-body" background-color="wheat" />
	 * </fo:simple-page-master> <!--<fo:simple-page-master
	 * master-name="chapterstart" xsl:use-attribute-sets="DINA4"
	 * margin-top="2cm" margin-bottom="4cm" margin-left="2cm"
	 * margin-right="4cm"> <fo:region-body region-name="xsl-region-body"
	 * background-color="yellow" margin-top="4cm" /> </fo:simple-page-master>-->
	 * <!--><! --> <fo:page-sequence-master master-name="header">
	 * <fo:repeatable-page-master-alternatives>
	 * <fo:conditional-page-master-reference master-reference="odd"
	 * odd-or-even="odd" /> <fo:conditional-page-master-reference
	 * master-reference="even" odd-or-even="even" />
	 * </fo:repeatable-page-master-alternatives> </fo:page-sequence-master>
	 * 
	 * <!--<fo:page-sequence-master master-name="chapter">
	 * <fo:repeatable-page-master-alternatives>
	 * <fo:conditional-page-master-reference master-reference="chapterstart"
	 * page-position="first" /> <fo:conditional-page-master-reference
	 * master-reference="odd" odd-or-even="odd" />
	 * <fo:conditional-page-master-reference master-reference="even"
	 * odd-or-even="even" /> </fo:repeatable-page-master-alternatives>
	 * </fo:page-sequence-master>-->
	 * 
	 * </fo:layout-master-set> </xsl:template>
	 */
	public Document getXslDoc(Document doc) {
		Element docRoot = doc.getRootElement();
		Element template = null;

		// find Element <xsl:template name="setupPagemasters">
		for (Object obj : docRoot.selectNodes("xsl:template").toArray()) {
			Element e = (Element) obj;
			Attribute a = e.attribute("name");
			if (a != null && a.getText().equalsIgnoreCase("setupPagemasters")) {
				template = e;
				break;
			}
		}

		if (template == null)
			return doc;

		String pageWidth = getValueByKey("page-layout-width");
		String pageHeight = getValueByKey("page-layout-height");
		String pageMarginTop = getValueByKey("page-layout-margin-top");
		String pageMarginBottom = getValueByKey("page-layout-margin-bottom");
		String pageMarginLeft = getValueByKey("page-layout-margin-left");
		String pageMarginRight = getValueByKey("page-layout-margin-right");
		String pageDoublesided = getValueByKey("page-layout-doublesided");

		// setupPagemasters
		Element layout_master_set = template.addElement("fo:layout-master-set");

		{
			// <fo:simple-page-master master-name="odd"
			Element simple_page_master = layout_master_set
					.addElement("fo:simple-page-master");
			// master-name="odd" margin-top="2cm" margin-bottom="3cm"
			// margin-left="2cm" margin-right="2cm"
			// <fo:region-body region-name="xsl-region-body"
			// background-color="red" />
			simple_page_master.addAttribute("page-height", pageHeight);
			simple_page_master.addAttribute("page-width", pageWidth);
			simple_page_master.addAttribute("master-name", "odd");
			simple_page_master.addAttribute("margin-top", pageMarginTop);
			simple_page_master.addAttribute("margin-bottom", pageMarginBottom);
			simple_page_master.addAttribute("margin-left", pageMarginLeft);
			simple_page_master.addAttribute("margin-right", pageMarginRight);
			Element region_body = simple_page_master
					.addElement("fo:region-body");
			region_body.addAttribute("region-name", "xsl-region-body");
			region_body.addAttribute("background-color", "red");
		}

		{
			if (pageDoublesided.equalsIgnoreCase("true")) { //double-sided printing
				
			// <fo:simple-page-master master-name="even"
			Element simple_page_master = layout_master_set
					.addElement("fo:simple-page-master");
			simple_page_master.addAttribute("page-height", pageHeight);
			simple_page_master.addAttribute("page-width", pageWidth);
			simple_page_master.addAttribute("master-name", "even");
			simple_page_master.addAttribute("margin-top", pageMarginTop);
			simple_page_master.addAttribute("margin-bottom", pageMarginBottom);
			
			simple_page_master.addAttribute("margin-left", pageMarginRight);
			simple_page_master.addAttribute("margin-right", pageMarginLeft);
			Element region_body = simple_page_master
					.addElement("fo:region-body");
			region_body.addAttribute("region-name", "xsl-region-body");
			region_body.addAttribute("background-color", "blue");
			}
		}

		{
			/*
			 * <fo:page-sequence-master master-name="header">
			 * <fo:repeatable-page-master-alternatives>
			 * <fo:conditional-page-master-reference master-reference="odd"
			 * odd-or-even="odd" /> <fo:conditional-page-master-reference
			 * master-reference="even" odd-or-even="even" />
			 * </fo:repeatable-page-master-alternatives>
			 * </fo:page-sequence-master>
			 *
			 * For odd pages use the page-layout "odd"; this is the standard
			 * for one-sided printing
			 * TODO: Add other layout elements
			 */
			Element page_sequence_master = layout_master_set
					.addElement("fo:page-sequence-master");
			page_sequence_master.addAttribute("master-name", "header");

			Element repeatable_page_master_alternatives = page_sequence_master
					.addElement("fo:repeatable-page-master-alternatives");

			Element conditional_page_master_reference = repeatable_page_master_alternatives
					.addElement("fo:conditional-page-master-reference");
			conditional_page_master_reference.addAttribute("master-reference",
					"odd");
			conditional_page_master_reference
					.addAttribute("odd-or-even", "odd");

			/*
			 * For even pages use the page-layout "even". This is of course only 
			 * interesting for double-sided printing
			 */
			if (pageDoublesided.equalsIgnoreCase("true")) { //double-sided printing
				Element conditional_page_master_reference2 = repeatable_page_master_alternatives
					.addElement("fo:conditional-page-master-reference");
				conditional_page_master_reference2.addAttribute("master-reference",
					"even");
				conditional_page_master_reference2.addAttribute("odd-or-even",
					"even");
			}
		}

		return doc;
	}
}
